package com.example.administrator.xxnapp.fragment.event;

import com.example.administrator.xxnapp.bean.EventBean;
import com.example.administrator.xxnapp.http.CustomStringCallback;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.Date;
import java.util.List;

import cn.mdruby.baselibrary.utils.DateUtil;

/**
 * Created by Administrator on 2018/5/12.
 */

public class EventPresenter {
    private IViewEvent mIView;
    private IModelEvent mIModel;

    public EventPresenter(IViewEvent mIView, IModelEvent mIModel) {
        this.mIView = mIView;
        this.mIModel = mIModel;
    }

    public void getEvent(int userId){
        mIModel.getEvent(userId).execute(new CustomStringCallback() {
            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                mIView.getEeventError(0,"请稍后重试");
            }

            @Override
            public void onSuccessString(Gson gson, String responseBody) {
                try {
                    JSONObject object = new JSONObject(responseBody);
                    if (object.getInt("result") == 200){
                        Type type = new TypeToken<List<EventBean>>() {
                        }.getType();
                        List<EventBean> eventBeans = gson.fromJson(object.getJSONArray("data").toString(),type);
                        mIView.getEeventSuccess(eventBeans);
                    }else {
                        mIView.getEeventError(object.getInt("result"),object.getString("message"));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void saveEvent(int userId,String date,String content,String  describe){
        EventBean bean = new EventBean();
        bean.setUserId(userId);
        bean.setDescribe(describe);
        bean.setContent(content);
        bean.setCreateDate(DateUtil.toLong(date));
        mIModel.saveEvent(bean)
                .execute(new CustomStringCallback() {
                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        mIView.saveEventError(0,"请稍后重试");
                    }

                    @Override
                    public void onSuccessString(Gson gson, String responseBody) {
                        try {
                            JSONObject object = new JSONObject(responseBody);
                            if (object.getInt("result") == 200){
                                mIView.saveEventSuccess("保存成功");
                            }else {
                                mIView.saveEventError(object.getInt("result"),object.getString("message"));
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }
}
